package com.chookss.home.gallery.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.GallerySearchEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.Utils;
import com.chookss.tools.bigImage.ImagePreview;
import com.chookss.tools.bigImage.bean.ImageInfo;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.Nullable;

public class GallerySearchPicAdapter extends BaseQuickAdapter<GallerySearchEntity.ImageListBean.ListBean, BaseViewHolderEx> {
    private Activity context;
    private int width, height;
    private int type = 1;// 0需要特殊处理，1不用

    public GallerySearchPicAdapter(int layoutResId, Activity context, @Nullable List<GallerySearchEntity.ImageListBean.ListBean> data, int type) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 30)) / 4;
        height = width * 195 / 165;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, GallerySearchEntity.ImageListBean.ListBean item) {
        ImageView ivBg = helper.getView(R.id.ivBg);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivBg.getLayoutParams();
        params.height = width;
        params.width = width;
        ivBg.setLayoutParams(params);
        if (type == 1) {
            GlideUtils.load(context, item.getImagePath(), ivBg, R.drawable.default_news);
        } else {
            if (getData().size() == 1) {
                GlideUtils.loadCustomRound(context, item.getImagePath(), ivBg, R.drawable.default_news, 15);
            } else if (getData().size() > 1 && getData().size() < 8) {
                if (helper.getItemPosition() == 0) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 4);
                } else if (helper.getItemPosition() == getData().size() - 1) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 5);
                } else {
                    GlideUtils.load(context, item.getImagePath(), ivBg, R.drawable.default_news);
                }
            } else if (getData().size() == 8) {
                if (helper.getItemPosition() == 0) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 0);
                } else if (helper.getItemPosition() == 3) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 1);
                } else if (helper.getItemPosition() == 4) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 2);
                } else if (helper.getItemPosition() == getData().size() - 1) {
                    GlideUtils.loadCustomRound2(context, item.getImagePath(), ivBg, 3);
                } else {
                    GlideUtils.load(context, item.getImagePath(), ivBg, R.drawable.default_news);
                }
            } else {
                GlideUtils.load(context, item.getImagePath(), ivBg, R.drawable.default_news);
            }
        }

        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ImageInfo> imageInfoList = new ArrayList<>();
                int index = helper.getItemPosition();
                for (int i = 0; i < getData().size(); i++) {
                    imageInfoList.add(dealData(getData().get(i)));
                }
                ImagePreview.getInstance()
                        .setContext(context)
                        // 设置从第几张开始看（索引从0开始）
                        .setIndex(index)
                        .setShowCloseButton(true)
                        .setToNewView(true)
                        .setLoadStrategy(ImagePreview.LoadStrategy.NetworkAuto)
                        .setImageInfoList(imageInfoList)
                        .start();
            }
        });
    }

    private ImageInfo dealData(GallerySearchEntity.ImageListBean.ListBean imagePathListBean) {
        ImageInfo imageInfo = new ImageInfo();
        String originUrl = imagePathListBean.getImagePath().split("\\?")[0];
//                    imageInfo.setThumbnailUrl(originUrl+"?x-oss-process=image/resize,m_lfit,w_720");
        imageInfo.setThumbnailUrl(originUrl);
        imageInfo.setOriginUrl(originUrl);
        imageInfo.setImageCode(imagePathListBean.getImageCode());
        if (Utils.isNull(imagePathListBean.getProvideEmployeeName())) {
            imageInfo.setName(imagePathListBean.getCreateEmployeeName());
        } else {
            imageInfo.setName(imagePathListBean.getProvideEmployeeName());
        }
        String date = imagePathListBean.getCreateTime();
        if (!Utils.isNull(date)) {
            if (date.length() >= 17) {
                imageInfo.setDay(Utils.getNormalYMDTime(date.substring(0, 10)));
                imageInfo.setMinutes(date.substring(11, 16));
                imageInfo.setDate(date.substring(0, 10));
            } else {
                imageInfo.setDay("");
                imageInfo.setMinutes("");
                imageInfo.setDate("");
            }
        } else {
            imageInfo.setDay("");
            imageInfo.setMinutes("");
            imageInfo.setDate("");
        }
        imageInfo.setLocation(imagePathListBean.getImageUploadLocationProvince() + " " + imagePathListBean.getImageUploadLocationCity());
        return imageInfo;
    }

}
